package br.com.simora.controller;

import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.jstl.core.Config;

import br.com.caelum.vraptor.Controller;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Result;
import br.com.simora.component.AcessSession;
import br.com.simora.execucao.Execucao;
import br.com.simora.execucao.ExecucaoDAO;
import br.com.simora.radio.Radio;

@Controller
public class HomeController extends BasicController {
	@Inject AcessSession acessSession;
	@Inject private HttpSession session;
	@Inject private ExecucaoDAO execucaoDAO;
	
	@Inject
    public HomeController(Result result, ResourceBundle bundle, ExecucaoDAO execucaoDAO) {
		this.result = result;
		this.bundle = bundle;
		this.execucaoDAO = execucaoDAO;
	}
	
	public HomeController(){}
	
	@PostConstruct
    private void init(){
    	injetaUsuario();
    }
	
    @Path(value = "/")
	public void index() {}
    @Path(value = "/comofunciona")
	public void comofunciona() {}
    @Path(value = "/assine")
	public void assine() {}
    @Path(value = "/contato")
	public void contato() {}
    @Path(value = "/sobre")
	public void sobre() {}
    @Path(value = "/login")
	public void login() {}
    @Path(value = "/minhasmusicas")
	public void minhasmusicas() {}
    @Path(value = "/meuplano")
	public void meuplano() {}
    @Path(value = "/cadastrousuario")
	public void cadastrousuario() {}
    
    @Path(value = "/changeLanguage/{language}")
    public void changeLanguage(String language){
    	if(language.equals("en_us")){
    		session.setAttribute("locale", "en_US");
    		Config.set(session, Config.FMT_LOCALE, new Locale("en", "US"));
    	}
    	if(language.equals("pt_br")){
    		session.setAttribute("locale", "pt_BR");
    		Config.set(session, Config.FMT_LOCALE, new Locale("pt", "BR"));
    	}
    	if(language.equals("es_es")){
    		session.setAttribute("locale", "es_es");
    		Config.set(session, Config.FMT_LOCALE, new Locale("es", "ES"));
    	}
    	redirect();
	}
    
    @Path(value = "/logout")
	public void logout() {
    	acessSession.setNome(null);
    	acessSession.setUsuario(null);
    	result.redirectTo(HomeController.class).index();
    }

	protected void redirect() {
		result.redirectTo(HomeController.class).index();
	}

	@SuppressWarnings("unchecked")
	@Path(value = "/indexAdmin")
	public void indexAdmin() {
		List<Radio> radios = (List<Radio>) execucaoDAO.recuperaTodos(Radio.class);
    	List<Execucao> execucao = new ArrayList<Execucao>();
    	for(Radio radio : radios){
    		Execucao e = execucaoDAO.recuperaUltimaExecucaoRadio(radio);
    		if(e != null){
    			execucao.add(e);
    		}
    	}
    	result.include("listaExecucoes", execucao);
	}

	@Path(value = "/indexUsuario")
	public void indexUsuario() {
		LocalDate initial = LocalDate.now();
		LocalDate start = initial.withDayOfMonth(1);
		Instant instant = start.atStartOfDay(ZoneId.systemDefault()).toInstant();
		Date res = Date.from(instant);
		LocalDate end = initial.withDayOfMonth(initial.lengthOfMonth());
		Instant instant2 = end.atStartOfDay(ZoneId.systemDefault()).toInstant();
		Date res2 = Date.from(instant2);
		result.include("listaMusicas", execucaoDAO.recuperaMusicasMaisTocadasPeriodo(res, res2));
		result.include("listaArtistas", execucaoDAO.recuperaArtistasMaisTocadosPeriodo(res, res2));
	}

}